import sys
from PySide.QtCore import *
from PySide.QtGui import *

import pybayes as pb
import numpy as np

_A=np.array([[1.]])
_C=np.array([[1.]]) 
_Q=np.array([[1.]])
_R=np.array([[1.]])
s_pdf=pb.GaussPdf(np.array([1.]), np.array([[1.]]))
kf=pb.KalmanFilter(A=_A,C=_C,Q=_Q,R=_R,state_pdf=s_pdf)
p=kf.posterior()

def update(x):
    kf.bayes(np.array([x]))
    return p.mean()[0],p.variance()[0]

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(320, 205)
        self.pushButton = QPushButton(Dialog)
        self.pushButton.setGeometry(QRect(210, 160, 75, 25))
        self.pushButton.setObjectName("pushButton")
        self.textBrowser = QTextBrowser(Dialog)
        self.textBrowser.setGeometry(QRect(10, 10, 300, 131))
        self.textBrowser.setObjectName("textBrowser")
        self.textEdit = QTextEdit(Dialog)
        self.textEdit.setGeometry(QRect(35, 160, 100, 25))
        self.textEdit.setObjectName("textEdit")

        self.retranslateUi(Dialog)
        QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        Dialog.setWindowTitle(QApplication.translate("Dialog", "卡尔曼滤波器", None, QApplication.UnicodeUTF8))
        self.pushButton.setText(QApplication.translate("Dialog", "更新", None, QApplication.UnicodeUTF8))

class MainUi(QDialog,Ui_Dialog):
    def __init__(self,parent = None):#parent默认为0
        super(MainUi,self).__init__(parent)#固定形式
        
        self.setupUi(self)
        self.connectSlot()
        
    def connectSlot(self):#连接逻辑和界面
        #self.textEdit.textChanged.connect(self.changeWord)#同步textEdit和textBrowser的内容
        self.pushButton.clicked.connect(self.changeWord)

    def clearText(self):
        self.textEdit.setText("")
    
    def changeWord(self):#更改textBrowse显示内容
        mean,var=update(float(self.textEdit.toPlainText()))
        _text = "<font color = blue>"+u"滤波值：%f<br />方差：%f"%(mean,var)+"</font>"
        self.textBrowser.setText(_text)

def main():
    app = QApplication(sys.argv)
    ui = MainUi()#没有给parent赋值
    ui.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()